<?php

class deleteProfileDocumentTypesAction extends sfAction
{

    private $profileDocumentTypeConfigurationService;

    public function getProfileDocumentTypeConfigurationService()
    {

        if (!($this->profileDocumentTypeConfigurationService instanceof ProfileDocumentTypeConfigurationService)) {
            $this->profileDocumentTypeConfigurationService = new ProfileDocumentTypeConfigurationService();
        }

        return $this->profileDocumentTypeConfigurationService;
    }

    public function setProfileDocumentTypeConfigurationService($profileDocumentTypeConfigurationService)
    {
        $this->profileDocumentTypeConfigurationService = $profileDocumentTypeConfigurationService;
    }

    public function execute($request)
    {

        $this->_checkAuthentication();

        $toDeleteIds = $request->getParameter('chkListRecord');

        if (!empty($toDeleteIds) && $request->isMethod('post')) {

            $result = $this->getProfileDocumentTypeConfigurationService()->deleteProfileDocumentTypes($toDeleteIds);

            if ($result) {
                $this->getUser()->setFlash('success', __(TopLevelMessages::DELETE_SUCCESS));
                $this->redirect('tlupim/viewProfileDocumentTypes');
            }

        }

    }

    protected function _checkAuthentication()
    {

        $user = $this->getUser()->getAttribute('user');

        if (!$user->isAdmin()) {
            $this->redirect('pim/viewPersonalDetails');
        }

    }

}
